package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CatalogDao;
import com.itheima.domain.store.Catalog;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.store.CatalogService;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CatalogServiceImpl implements CatalogService {

    @Override
    public PageInfo<Catalog> findAll(String pageStr, String sizeStr) {
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(sizeStr)) {
            size = Integer.parseInt(sizeStr);
        }
        PageHelper.startPage(page, size);
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        List<Catalog> list = dao.findAll();
        return new PageInfo<>(list);
    }

    @Override
    public void save(Catalog catalog) {
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        String id = UUID.randomUUID().toString().replace("-", "");
        catalog.setId(id);
        catalog.setCreateTime(new Date());
        dao.save(catalog);

    }

    @Override
    public Catalog findById(String id) {
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        return dao.findById(id);

    }

    @Override
    public void update(Catalog catalog) {
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        dao.update(catalog);
    }

    @Override
    public void delete(String id) {
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        dao.delete(id);
    }

    @Override
    public List<Catalog> findAll() {
        CatalogDao dao = DaoInstanceFactory.getMapper(CatalogDao.class);
        return dao.findAll();
    }

}
